package com.ffcs.cn.action.map.layersetting;

import com.supermap.data.Dataset;
import com.supermap.data.DatasetType;
import com.supermap.desktop.core.Application;
import com.supermap.desktop.core.utilties.DatasetUtilities;
import com.supermap.mapping.Layer;
import org.pushingpixels.substance.api.renderers.SubstanceDefaultTreeCellRenderer;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 自定义封装的图层树
 * 该树隐藏了根节点，并且只有一层节点，每一个节点都是一个图层
 *
  */
public class LayerTree extends JTree {

	// 根节点
	private final DefaultMutableTreeNode root = new DefaultMutableTreeNode();
	// 树模型
	private final DefaultTreeModel model = new DefaultTreeModel(root);

	public LayerTree() {
		// 隐藏根节点，使图层树始终保持只有一层
		setRootVisible(false);
		setModel(model);
		// 使用桌面统一的JTree的风格
		setCellRenderer(new SubstanceDefaultTreeCellRenderer() {
			@Override
			public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
				super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
				try {
					DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
					if (node.getUserObject() != null && node.getUserObject() instanceof Layer) {
						Layer layer = ((Layer) node.getUserObject());
						this.setText(layer.getCaption());
						this.setIcon(DatasetUtilities.getDatasetTypeIcon(layer.getDataset().getType()));
						this.setHorizontalAlignment(SwingConstants.LEADING);
					}
				} catch (Exception e) {
					Application.getActiveApplication().getOutput().output(e);
				}
				return this;
			}
		});
	}

	/**
	 * 添加一个节点
	 *
	 * @param layer 图层
	 */
	public void addLayer(Layer layer) {
		if (layer == null || !isSupportedDataset(layer.getDataset())) {
			return;
		}
		DefaultMutableTreeNode node = new DefaultMutableTreeNode(layer);
		model.insertNodeInto(node, root, root.getChildCount());
	}

	/**
	 * 删除选中节点
	 */
	public void removeSelectedLayers() {
		TreePath[] paths = getSelectionPaths();
		if (paths != null && paths.length > 0) {
			DefaultTreeModel model = (DefaultTreeModel) this.getModel();
			for (TreePath selectionPath : paths) {
				//移除选中节点
				model.removeNodeFromParent((DefaultMutableTreeNode) selectionPath.getLastPathComponent());
			}
		}
		// 删除完了之后默认选中最后一个节点
		if (root.getChildCount() > 0) {
			setSelectedLayer(root.getChildCount() - 1);
		}
	}

	/**
	 * 获取图层索引号
	 *
	 * @param layer 图层
	 * @return 索引序号
	 */
	public int getLayerIndex(Layer layer) {
		for (int i = 0; i < root.getChildCount(); i++) {
			if (layer == ((Layer) ((DefaultMutableTreeNode) root.getChildAt(i)).getUserObject())) {
				return i;
			}
		}
		return -1;
	}
	/**
	 * 设置选中图层
	 *
	 * @param index 索引号，从0计数
	 */
	public void setSelectedLayer(int index) {
		setSelectedLayer(new int[]{index});
	}

	/**
	 * 设置选中图层
	 *
	 * @param indices 索引号
	 */
	public void setSelectedLayer(int[] indices) {
		if (indices == null) {
			return;
		}
		List<TreePath> selectedPathsList = new ArrayList<>();
		for (int index : indices) {
			if (index < 0 || index >= root.getChildCount()) {
				continue;
			}
			selectedPathsList.add(new TreePath(((DefaultMutableTreeNode) root.getChildAt(index)).getPath()));
		}
		this.setSelectionPaths(selectedPathsList.toArray(new TreePath[0]));
	}
	/**
	 * 移除选中图层
	 *
	 * @param indices 索引号
	 */
	public void removeSelectedLayer(int[] indices) {
		if (indices == null) {
			return;
		}
		List<TreePath> selectedPathsList = new ArrayList<>();
		for (int index : indices) {
			if (index < 0 || index >= root.getChildCount()) {
				continue;
			}
			selectedPathsList.add(new TreePath(((DefaultMutableTreeNode) root.getChildAt(index)).getPath()));
		}
		this.removeSelectionPaths(selectedPathsList.toArray(new TreePath[0]));
	}

	/**
	 * 获取选中的图层
	 *
	 * @return 选中的图层
	 */
	public Layer[] getSelectedLayers() {
		TreePath[] paths = getSelectionPaths();
		if (paths == null || paths.length == 0) {
			return new Layer[0];
		}
		Layer[] selectedLayers = new Layer[paths.length];
		for (int i = 0; i < paths.length; i++) {
			selectedLayers[i] = ((Layer) ((DefaultMutableTreeNode) paths[i].getLastPathComponent()).getUserObject());
		}
		return selectedLayers;
	}

	/**
	 * 获取图层树所有图层
	 *
	 * @return layer[]
	 */
	public Layer[] getAllLayers() {
		Layer[] layers = new Layer[root.getChildCount()];
		for (int i = 0; i < root.getChildCount(); i++) {
			layers[i] = ((Layer) ((DefaultMutableTreeNode) root.getChildAt(i)).getUserObject());
		}
		return layers;
	}

	/**
	 * 图层树支持的数据集
	 *
	 * @param dataset 数据集
	 * @return 是否支持
	 */
	public static boolean isSupportedDataset(Dataset dataset) {
		if (dataset == null) {
			return false;
		}
		DatasetType datasetType = dataset.getType();
		return datasetType != null
				&& datasetType != DatasetType.TABULAR
				&& datasetType != DatasetType.GRIDCOLLECTION
				&& datasetType != DatasetType.IMAGECOLLECTION
				&& datasetType != DatasetType.PARAMETRICLINE
				&& datasetType != DatasetType.PARAMETRICREGION
				&& datasetType != DatasetType.UNKNOWN
				&& datasetType != DatasetType.TEXT;
	}

	/**
	 * 返回图层数量
	 *
	 * @return 图层数量
	 */
	public int getLayerCount() {
		return root.getChildCount();
	}
}
